package com.itheima.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @Author itheima
 * @Date 2022/4/27 11:25
 */
@Configuration
@ComponentScan("com.itheima")
public class SpringMvcConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        //  "/WEB-INF/pages/"+"success"+".jsp"
        registry.jsp("/WEB-INF/pages/", ".jsp");
        super.configureViewResolvers(registry);
    }

    /**
     * 对静态资源放行：如果不对静态资源放行，springmvc会默认对请求静态资源url当成controller方法处理。
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 1.addResourceHandler("/images/**") 浏览器发送请求；例如：http://localhost:8080/images/aa/itheima.png
         *   例如：http://localhost:8080/images/itheima.png
         * 2.addResourceLocations("/images/");告诉springmvc请求静态资源存放位置
         */
        registry.addResourceHandler("/images/*").addResourceLocations("/images/");
        super.addResourceHandlers(registry);
    }

    /**
     * CommonsMultipartResolver作用：就是将上传的文件进行获取，给变量赋值MultipartFile file=？File(接收到文件)
     * 注意：CommonsMultipartResolver放入到spring容器中，该bean的名称是固定的multipartResolver
     * 多媒体解析器
     *
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        //设置上传最大文件大小，单位为字节，例如：这里设置的100M
        multipartResolver.setMaxUploadSize(10485760L);
        //设置上传文件的编码为“UTF-8”
        multipartResolver.setDefaultEncoding("utf-8");
        return multipartResolver;
    }

}
